package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Excel;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.domain.model.LoginUser;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.ImportFailEnum;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.*;
import com.member.shop.service.ISysBizImportFailService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 【导入失败】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-01-18
 **/
@RestController
@RequestMapping("/shop/fail")
public class SysBizImportFailController extends BaseController {
    @Resource
    private ISysBizImportFailService sysBizImportFailService;

    /**
     * 查询导入失败列表
     */
    @PreAuthorize("@ss.hasPermi('shop:fail:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizImportFail sysBizImportFail) {
        startPage();
        LambdaQueryWrapper<SysBizImportFail> queryWrapper = new LambdaQueryWrapper<SysBizImportFail>();
        List<SysBizImportFail> list = sysBizImportFailService.selectSysBizImportFailList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出导入失败列表
     */
    @PreAuthorize("@ss.hasPermi('shop:fail:export')")
    @Log(title = "导入失败", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizImportFail sysBizImportFail) {
        LambdaQueryWrapper<SysBizImportFail> queryWrapper = new LambdaQueryWrapper<SysBizImportFail>();
        List<SysBizImportFail> list = sysBizImportFailService.selectSysBizImportFailList(queryWrapper);
        ExcelUtil<SysBizImportFail> util = new ExcelUtil<SysBizImportFail>(SysBizImportFail.class);
        util.exportExcel(response, list, "导入失败数据");
    }

    /**
     * 获取导入失败详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:fail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizImportFailService.selectSysBizImportFailById(id));
    }

    /**
     * 新增导入失败
     */
    @PreAuthorize("@ss.hasPermi('shop:fail:add')")
    @Log(title = "导入失败", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizImportFail sysBizImportFail) {
        return toAjax(sysBizImportFailService.insertSysBizImportFail(sysBizImportFail));
    }

    /**
     * 修改导入失败
     */
    @PreAuthorize("@ss.hasPermi('shop:fail:edit')")
    @Log(title = "导入失败", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizImportFail sysBizImportFail) {
        return toAjax(sysBizImportFailService.updateSysBizImportFail(sysBizImportFail));
    }

    /**
     * 删除导入失败
     */
    @PreAuthorize("@ss.hasPermi('shop:fail:remove')")
    @Log(title = "导入失败", businessType = BusinessType.DELETE)
    @DeleteMapping("/{type}")
    public AjaxResult remove(@PathVariable("type") Integer type) {
        return toAjax(sysBizImportFailService.deleteSysBizImportFailByIds(type));
    }



    /**
     * 会员导出导入失败列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:register')")
    @Log(title = "导入失败", businessType = BusinessType.EXPORT)
    @PostMapping("/registerExport")
    public void registerExport(HttpServletResponse response, SysBizImportFail sysBizImportFail) {
        LoginUser loginUser = getLoginUser();
        LambdaQueryWrapper<SysBizImportFail> queryWrapper = new LambdaQueryWrapper<SysBizImportFail>();
        queryWrapper.eq(SysBizImportFail::getUserId,loginUser.getUserId());
        queryWrapper.eq(SysBizImportFail::getType, ImportFailEnum.REGISTER_MEMBER_IMPORT.getKey());
        List<SysBizImportFail> list = sysBizImportFailService.selectSysBizImportFailList(queryWrapper);
        List<SysBizImportFailRegister> list2 = new ArrayList<SysBizImportFailRegister>();
        for (SysBizImportFail bizImportFail : list) {
            SysBizImportFailRegister sysBizImportFailRegister = new SysBizImportFailRegister();
            sysBizImportFailRegister.setUserPhone(bizImportFail.getUserPhone());
            sysBizImportFailRegister.setFailureReason(bizImportFail.getFailureReason());
            list2.add(sysBizImportFailRegister);
        }
        ExcelUtil<SysBizImportFailRegister> util = new ExcelUtil<SysBizImportFailRegister>(SysBizImportFailRegister.class);
        util.exportExcel(response, list2, "导入失败数据");
    }

    /**
     * 充值导出导入失败列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:recharge')")
    @Log(title = "导入失败", businessType = BusinessType.EXPORT)
    @PostMapping("/rechargeExport")
    public void rechargeExport(HttpServletResponse response, SysBizImportFail sysBizImportFail) {
        LoginUser loginUser = getLoginUser();
        LambdaQueryWrapper<SysBizImportFail> queryWrapper = new LambdaQueryWrapper<SysBizImportFail>();
        queryWrapper.eq(SysBizImportFail::getUserId,loginUser.getUserId());
        queryWrapper.eq(SysBizImportFail::getType, ImportFailEnum.RECHARGE_IMPORT.getKey());
        List<SysBizImportFail> list = sysBizImportFailService.selectSysBizImportFailList(queryWrapper);
        List<SysBizImportFailRecharge> list2 = new ArrayList<SysBizImportFailRecharge>();
        for (SysBizImportFail bizImportFail : list) {
            SysBizImportFailRecharge sysBizImportFailRegister = new SysBizImportFailRecharge();
            sysBizImportFailRegister.setUserPhone(bizImportFail.getUserPhone());
            sysBizImportFailRegister.setRechargeBalance(bizImportFail.getRechargeBalance());
            sysBizImportFailRegister.setFailureReason(bizImportFail.getFailureReason());
            list2.add(sysBizImportFailRegister);
        }
        ExcelUtil<SysBizImportFailRecharge> util = new ExcelUtil<SysBizImportFailRecharge>(SysBizImportFailRecharge.class);
        util.exportExcel(response, list2, "导入失败数据");
    }


    /**
     * 批量发货导出导入失败列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:order')")
    @Log(title = "导入失败", businessType = BusinessType.EXPORT)
    @PostMapping("/orderExport")
    public void orderExport(HttpServletResponse response, SysBizImportFail sysBizImportFail) {
        LoginUser loginUser = getLoginUser();
        LambdaQueryWrapper<SysBizImportFail> queryWrapper = new LambdaQueryWrapper<SysBizImportFail>();
        queryWrapper.eq(SysBizImportFail::getUserId,loginUser.getUserId());
        queryWrapper.eq(SysBizImportFail::getType, ImportFailEnum.ORDER_SHIPPING.getKey());
        List<SysBizImportFail> list = sysBizImportFailService.selectSysBizImportFailList(queryWrapper);
        List<SysBizImportFailOrder> list2 = new ArrayList<SysBizImportFailOrder>();
        for (SysBizImportFail bizImportFail : list) {
            SysBizImportFailOrder sysBizImportFailOrder = new SysBizImportFailOrder();
            sysBizImportFailOrder.setExpressCompany(bizImportFail.getExpressCompany());
            sysBizImportFailOrder.setOrderCode(bizImportFail.getOrderCode());
            sysBizImportFailOrder.setExpressNo(bizImportFail.getExpressNo());
            sysBizImportFailOrder.setFailureReason(bizImportFail.getFailureReason());
            list2.add(sysBizImportFailOrder);
        }
        ExcelUtil<SysBizImportFailOrder> util = new ExcelUtil<SysBizImportFailOrder>(SysBizImportFailOrder.class);
        util.exportExcel(response, list2, "导入失败数据");
    }



    /**
     * 批量导出导入失败列表
     */
    @Log(title = "导入失败", businessType = BusinessType.EXPORT)
    @PostMapping("/evaluateExport")
    public void evaluateExport(HttpServletResponse response, SysBizImportFail sysBizImportFail) {
        LoginUser loginUser = getLoginUser();
        LambdaQueryWrapper<SysBizImportFail> queryWrapper = new LambdaQueryWrapper<SysBizImportFail>();
        queryWrapper.eq(SysBizImportFail::getUserId,loginUser.getUserId());
        queryWrapper.eq(SysBizImportFail::getType, ImportFailEnum.EVALUATION_IMPORT.getKey());
        List<SysBizImportFail> list = sysBizImportFailService.selectSysBizImportFailList(queryWrapper);
        List<SysBizImportFailEvaluate> list2 = new ArrayList<SysBizImportFailEvaluate>();

        for (SysBizImportFail bizImportFail : list) {
            SysBizImportFailEvaluate sysBizImportFailEvaluate = new SysBizImportFailEvaluate();
            sysBizImportFailEvaluate.setNickName(bizImportFail.getNickName());
            sysBizImportFailEvaluate.setGoodsCode(bizImportFail.getGoodsCode());
            sysBizImportFailEvaluate.setScore(bizImportFail.getScore());
            sysBizImportFailEvaluate.setSpecsKey(bizImportFail.getSpecsKey());
            sysBizImportFailEvaluate.setContentText(bizImportFail.getContentText());
            sysBizImportFailEvaluate.setFailureReason(bizImportFail.getFailureReason());
            list2.add(sysBizImportFailEvaluate);
        }
        ExcelUtil<SysBizImportFailEvaluate> util = new ExcelUtil<SysBizImportFailEvaluate>(SysBizImportFailEvaluate.class);
        util.exportExcel(response, list2, "导入失败数据");
    }
}
